package com.xxxx.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.server.mapper.DepartmentMapper;
import com.xxxx.server.pojo.Department;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhoubin
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> selectDepart(String Depname) {
        List<Department> list = departmentMapper.selectDepartNameDao(Depname);

        List<Department> listout=new ArrayList<>();
        int num=0;
        for (Department i : list) {//遍历每一个元素

            List<Department> list01 = null;//初始化
            if (i.getParentId()==-1){//通过判断得出（-1）股东会


                list.set(num,part(list,i,list01));//更新list中的数据利用封装过的方法part

                listout.add(list.get(num));//承接数据用于返还到controller层

                num++;//计数器
            }


        }

        return listout;
    }

    @Override
    public List<Department> selectDepartmentAll() {
        return departmentMapper.selectDepartmentAll(-1);
    }


    public Department part(List<Department> list , Department i ,List<Department> list01 ){

            if (i.getParent()){//判断该元素是否是一个父级节点（拥有下级）


                list01 = new ArrayList<>();//初始化
                for (Department j : list) {//当i拥有下级再次遍历将对应下级放入容器

                    if (j.getParentId().equals(i.getId())) {//找到子类
                        j=part(list,j,list01);//递归此方法
                        list01.add(j);
                    }
                }
                i.setChildren(list01);

                return i;
            }
            return i;
        }


    @Override
    public RespBean addDepart(Department department) {
        Integer i= departmentMapper.addDepartDao(department);

        if (i>0){
            departmentMapper.updataDepartDao_parentid(department);
            return  RespBean.success("添加成功");

        }
        return RespBean.error("添加失败");
    }

    @Override
    public RespBean deletDepart(String id) {

        Department department=departmentMapper.selectDepartment01(id);//获取当前节点的上层ID
        Integer i= departmentMapper.detelDepartDao(id);
        List<Department> list = departmentMapper.selectDepartNameDao(null);//找到删除当前节点后的信息集合

        if (i>0){
            int num=0;
            for ( Department j:list){//查询是否还有子节点
                if (department.getParentId().equals(j.getParentId())){
                   num++;//记录子节点数
                }

            }

            if (num==0){//如果子节点为0
                departmentMapper.updataDepartDao_parentid02(department.getParentId());//通过获取的上层的ID更新其父节点的属性取消其是否为父类
            }

            return  RespBean.success("删除成功");

        }
        return RespBean.error("删除失败");
    }
}
